/**
 * 弹出授权设置
 */
const putOpenSetting = () => {
  wx.openSetting({
    complete: (res) => {
      console.log(res)
    }
  })
}
/**
 * 扫一扫
 */
const scanCode = function () {
  wx.scanCode({
    scanType: ['qrCode'],
    success: res => {
      if (res.result) {
        var str = decodeURIComponent(res.result)
        var index = str.lastIndexOf("\/");
        str = str.substring(index + 1, str.length);
        wx.removeStorageSync('qrCodeFeast');
        wx.setStorageSync('qrCode', str);
        wx.redirectTo({
          url: '/pages/scan/scan',
        })
      }

    }
  })
}
/**
 * 正在加载
 * @param {*} title 
 */
const showLoading = (title) => {

  setTimeout( () => {
    wx.showToast({
      title: title,
      icon: "loading",
    });
    setTimeout( () =>{
      wx.hideToast();  
    },4000)
  },0);
  // wx.showToast({
  //   title: title,
  //   icon: 'loading',
  //   duration: 8000,
  //   mask: true
  // }, 8000);
}
/**
 * 取消加载
 */
const cancelLoading = () => {
  wx.hideToast();
}
/**
 * 判断网络
 * @param {*} callback 
 */

const judgeNetworkStatus = (callback) => {
  wx.getNetworkType({
    success(res) {
      const networkType = res.networkType
      if ("none" == networkType) {
        wx.showLoading({
          title: '网络不给力~',
          mask: true
        })
        judgeNetworkStatus(callback);
      } else {
        wx.hideLoading()
        callback(networkType)
      }
    },
    fail(err) {},
    complete(cpe) {}
  })
}
module.exports = {
  putOpenSetting,
  scanCode,
  thirdAppId: 'wx3a1ea9917d14931b',
  showLoading,
  cancelLoading,
  judgeNetworkStatus
};